package com.study.demo_resttemplate.config;

import com.study.demo_resttemplate.demo.SDKInteface;
import com.study.demo_resttemplate.entity.sdkentity.SDKApiSign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * @Name: UserAgentInterceptor
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/7/18 15:20
 * @Remark: 特殊说明
 * @Version: 1.0
 */
public class UserAgentInterceptor implements ClientHttpRequestInterceptor {

    @Autowired
    SDKInteface sdkInteface;

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {
        String secretId="s648006";
        String secretKey ="f89aa8d260a2914ec0cb35cd8d328ff65b99a6a2ccf8593992d90725dfb99ac6";
        SDKApiSign sdkApiSign = sdkInteface.generateApiSign(secretKey,secretId);
        HttpHeaders headers = request.getHeaders();
        headers.add("Secret-Id",secretId);
        headers.add("Signature-Time",sdkApiSign.getSignData().getSignatureTime());
        headers.add("Signature",sdkApiSign.getSignData().getSignature());
        headers.add("Nonce",sdkApiSign.getSignData().getNonce().toString());
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        return execution.execute(request, body);
    }
}
